package com.fivefiveseven;

/**
 * @author ピカチュウ(皮卡啾)
 * @date 2021/7/10 20:47
 * @Description: 557. 反转字符串中的单词 III
 */
public class FiveFiveSeven {
    public String reverseWords(String s) {
        // 字符串加入空格，方便反转最后一次的数据
        s = s + " ";
        char[] chars = s.toCharArray();

        for (int i = 0, start = 0, end = 0; i < chars.length; i++) {
            if (' ' == chars[i]) {
                end = i - 1;
                while (start < end) {
                    char tmp = chars[start];
                    chars[start] = chars[end];
                    chars[end] = tmp;
                    start++;
                    end--;
                }
                start = i + 1;
            }
        }

        for (char c : chars) {
            System.out.println(c);
        }

        // 将开头加入的空格截取掉
        return new String(chars).substring(0, chars.length - 1);
    }

}
